/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ipercube.sample;

import com.ipercube.Dimension;
import com.ipercube.impl.simple.SimpleRecord;
import com.ipercube.DataSourceModel;
import com.ipercube.model.Navigator;
import com.ipercube.model.Record;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author tartur
 */
public class SampleDataModel implements DataSourceModel {

    public static final Dimension REGION = new Dimension("Region", String.class);
    public static final Dimension SEMESTER = new Dimension("Semester", Integer.class);
    public static final Dimension YEAR = new Dimension("Year", Integer.class);
    public static final Dimension INCOME = new Dimension("Income", Double.class);
    private static final List<Dimension> dims = Arrays.asList(REGION, SEMESTER, YEAR, INCOME);

    private List<Record> records = null;

    public SampleDataModel(){
        records = new ArrayList<Record>();
        loadData();
    }

    private void loadData(){
        int i = 1;
        records.add(createRecord(i++,"Region1",1,2008,10));
        records.add(createRecord(i++,"Region1",2,2008,20));
        records.add(createRecord(i++,"Region1",1,2009,30));
        records.add(createRecord(i++,"Region1",2,2008,25));
        records.add(createRecord(i++,"Region1",2,2009,10));
        records.add(createRecord(i++,"Region2",1,2008,15));
        records.add(createRecord(i++,"Region2",2,2008,25));
        records.add(createRecord(i++,"Region2",1,2009,40));
        records.add(createRecord(i++,"Region2",2,2008,5));
        records.add(createRecord(i++,"Region2",2,2009,14));
        records.add(createRecord(i++,"Region3",1,2008,18));
        records.add(createRecord(i++,"Region3",2,2008,2));
        records.add(createRecord(i++,"Region3",1,2009,30));
        records.add(createRecord(i++,"Region3",2,2008,5));
        records.add(createRecord(i++,"Region3",2,2009,5));

    }

    private Record createRecord(int id, String region, int semester, int year, double income){
        SimpleRecord r = new SimpleRecord(id,SampleDataModel.dims.toArray(new Dimension[4]));
        r.setValue(region, REGION);
        r.setValue(semester, SEMESTER);
        r.setValue(year, YEAR);
        r.setValue(income, INCOME);
        return r;
    }

    public void insert(Record r){
        records.add(r);
    }

    public void delete(Record r){
        records.remove(r);
    }

    public List<Dimension> getDimensions() {
        return dims;
    }

    public Navigator<Record> getNavigator() {
        return new NavigatorImpl();
    }

    public Iterator<Record> iterator() {
        return getNavigator();
    }

    private class NavigatorImpl implements Navigator<Record>{

        private int current = -1;

        public boolean hasPrevious() {
            return current > 0;
        }

        public Record previous() {
            if(!hasPrevious())
                throw new RuntimeException("There is no previous record");
            return records.get(--current);
        }

        public boolean hasNext() {
            return current < records.size()-1;
        }

        public Record next() {
            if(!hasNext())
                throw new RuntimeException("There is no next record");
            return records.get(++current);
        }

        public boolean setCursorAt(Record element) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public Record first() {
            current = 0;
            return records.get(current);
        }

        public Record last() {
            current = records.size() - 1;
            return records.get(current);
        }

        public void remove() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public Record getById(Serializable id) {
            Record res = null;
            for (Record r : records) {
                if(r.getId().equals(id)){
                    res = r;
                    break;
                }
            }
            return res;
        }

    }

}
